#coding:utf-8
from BeautifulSoup import BeautifulSoup
from urlparse import urlparse,urljoin


def parserLink(baseurl,pageContent):
    soup = BeautifulSoup(pageContent)
    soup.prettify()
    outlinks=[]
    for anchor in soup.findAll('a', href=True):
        url=anchor['href']
        if(url.startswith("javascript")):
            continue
        urlScheme=urlparse(url)
        if(urlScheme.scheme=="" and urlScheme.netloc==""):
            url=urljoin(baseurl,url.strip())
        
        outlinks.append(url)
        
    return outlinks


def parserLinkandAnchor(baseurl,pageContent): 
    soup = BeautifulSoup(pageContent)
    soup.prettify()
    outlinks=[]
    for anchor in soup.findAll('a', href=True):
        url=anchor['href']
        if(url.startswith("javascript")):
            continue
        urlScheme=urlparse(url)
        if(urlScheme.scheme=="" and urlScheme.netloc==""):
            url=urljoin(baseurl,url.strip())
       

        anchortext=anchor.text 
        outlinks.append((url,anchortext))
        
    return outlinks

def parserTitleandAnchor(baseurl,pageContent): 
    soup = BeautifulSoup(pageContent)
    soup.prettify()
    outlinks=[]
    titleblock=soup.find("title")
    if(titleblock):
        title=titleblock.text
    else:
        title="" 
    for anchor in soup.findAll('a', href=True):
        url=anchor['href']
        if(url.startswith("javascript")):
            continue
        urlScheme=urlparse(url)
        if(urlScheme.scheme=="" and urlScheme.netloc==""):
            url=urljoin(baseurl,url.strip())
       

        anchortext=anchor.text 
        outlinks.append((url,anchortext))
        
    return [title,outlinks]



if __name__=='__main__':
    import sys
    page=open(sys.argv[1]).read()
    outlinks=parserTitleandAnchor('',page)
    print len(outlinks)
    print outlinks[0]   


